package com.bt.dmdb.hibernate.interceptor;

import java.io.Serializable;
import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import org.joda.time.LocalDateTime;
import com.bt.dmdb.model.ChangedOnEntity;

@SuppressWarnings("serial")
public class ChangedOnInterceptor extends EmptyInterceptor {

    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
        if (entity instanceof ChangedOnEntity) {
            int index = ArrayUtils.indexOf(propertyNames, "changedOn");
            state[index] = new LocalDateTime();
            return true;
        }
        return false;
    }
}
